import Vue from 'vue'
import VueRouter from 'vue-router'
import {
  getToken
} from "@/utils/auth";
import wxlogin from '@/views/login/WxLogin';
import index from '@/views/home/Index';

Vue.use(VueRouter)

const routes = [
  {
    path: "/wxlogin",
    name: "wxlogin",
    component: wxlogin,
    meta: {
      login: false
    }
  },
  {
    path: "/",
    redirect: "/index"
  },
  {
    path: '/',
    name: 'layout',
    component: () => import('@/views/layout/Index'),
    children: [{
      path: '/index',
      name: 'index',
      component: index,
      meta: {
        title: '首页'
      }
    }, {
      path: '/upgrade',
      name: 'upgrade',
      component: () => import('@/views/upgrade/Index'),
      meta: {
        title: '升级'
      }
    },
    {
      path: '/my',
      name: 'my',
      component: () => import('@/views/my/Index'),
      meta: {
        title: '我的'
      }
    }]
  },
  {
    path: '/upgradeDetail',
    name: 'upgradeDetail',
    component: () => import('@/views/upgrade/Detail'),
    meta: {
      title: '会员升级'
    }
  },
  {
    path: '/projectDetail',
    name: 'projectDetail',
    component: () => import('@/views/projectDetail/Index'),
    meta: {
      title: '详情'
    }
  },
  {
    path: '/wallet',
    name: 'wallet',
    component: () => import('@/views/my/Wallet'),
    meta: {
      title: '我的钱包'
    }
  },
  {
    path: '/withdraw',
    name: 'withdraw',
    component: () => import('@/views/my/Withdraw'),
    meta: {
      title: '提现'
    }
  },
  {
    path: '/orderList',
    name: 'orderList',
    component: () => import('@/views/my/OrderList'),
    meta: {
      title: '订单管理'
    }
  },
  {
    path: '/aboutUs',
    name: 'aboutUs',
    component: () => import('@/views/my/AboutUs'),
    meta: {
      title: '关于我们'
    }
  },
  {
    path: '/team',
    name: 'team',
    component: () => import('@/views/my/Team'),
    meta: {
      title: '我的团队'
    }
  },
  {
    path: '/inviteFriend',
    name: 'inviteFriend',
    component: () => import('@/views/my/InviteFriend'),
    meta: {
      title: '邀请好友'
    }
  },
  {
    path: '/addressList',
    name: 'addressList',
    component: () => import('@/views/my/AddressList'),
    meta: {
      title: '收货地址'
    }
  },
  {
    path: '/addressEdit',
    name: 'addressEdit',
    component: () => import('@/views/my/AddressEdit'),
    meta: {
      title: '编辑收货地址'
    }
  }

]

const router = new VueRouter({
  mode: 'hash', //后端支持可开
  base: process.env.BASE_URL,
  routes
})
router.beforeEach((to, from, next) => {
  let token = getToken();

  if (((token == "undefined") || !token) && to.name != 'wxlogin') {
    return next({
      path: "/wxlogin",
      query: {
        fromName: to.name
      }
    });
  } else {
    next();
  }
});
router.afterEach(to => {
  if (to.meta.title) {
    document.title = to.meta.title;
  }
});
export default router
